require(["config"],function(){
	require(["jquery","cookie"],function($){
		function Login(){
			this.addListener();
		}

		$.extend(Login.prototype,{
			//注册时间监听
			addListener:function(){
//				console.log($("#middle .form .smdl"));
				//注册登录/扫码登录切换
				$("#middle .form .smdl").on("click",this.saomaLogin);
				$("#middle .form .zcdl").on("click",this.zhuceLogin);
				$("#middle .saoma").on("mouseenter",this.saomaEnter);
				$("#middle .saoma").on("mouseleave",this.saomaLeave);
				//注册登录
				$("#middle .login-form").on("click",$.proxy(this.loginHandler,this));
			},
			saomaLogin:function(){
				$("#middle .ewm").show();
				$("#middle form").hide();
				$("#middle .smdl").css({"color":"#e4393c","font-weight":"bold"});
				$("#middle .zcdl").css("color","#666666");
			},
			zhuceLogin:function(){
				$("#middle .ewm").hide();
				$("#middle form").show();
				$("#middle .zcdl").css({"color":"#e4393c","font-weight":"bold"});
				$("#middle .smdl").css({"color":"#666666","font-weight":"normal"});
			},
			saomaEnter:function(){
				$("#middle .shouji").show();
			},
			saomaLeave:function(){
				$("#middle .shouji").hide();
			},
			//登录处理
			loginHandler:function(){
				//获取表单中带提交的数据，序列化
				var data=$(".info_form").serialize();
				//ajax提交数据
				var url="http://localhost/E-commerce_project/api/login.php";
				$.post(url,data,this.loginSuccessHandler,"json");
				return false;
			},
			//登录提交ajax请求成功的回调
			loginSuccessHandler:function(data){
				console.log(data);
				if (data.res_code === 1) { // 登录成功
					// 将登录成功的用户信息保存到 cookie 中
					$.cookie.json = true;
					$.cookie("login-user", data.res_body.phone, {path: "/"});
//					// 跳转页面
					location = "/";
				} else { // 登录失败
					$("#middle .cuowu").removeClass("hidden");
				}
			}
		});
		
		new Login();
	});
});
